package com.whp.yky.yky.controller;


import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whp.yky.yky.entity.Notice;
import com.whp.yky.yky.entity.poentity.Document;
import com.whp.yky.yky.response.ResponseData;
import com.whp.yky.yky.service.impl.NoticeServiceImpl;
import com.whp.yky.yky.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zwq
 * @since 2021-03-02
 */
@RestController
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeServiceImpl noticeService;

    @PostMapping("/getNoticeInfo")
    public ResponseData getNoticeInfo( @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        IPage<Notice> pageList = this.noticeService.getNoticeInfo(new Page( (long)pageNum,(long)pageSize));
        res.setData(pageList);
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @PostMapping("/getNoticeDetailInfo")
    public ResponseData getNoticeDetailInfo( @RequestParam Integer id){
        ResponseData res = new ResponseData();
        res.setCode(20000);
        res.setData(noticeService.getNoticeDetailInfo(id));
//        res.setTotal(documentServiceImpl.getDocumentInfo(document).size());

        return res;

    }

    @PostMapping("/insertNoticeDetailInfo")
    public ResponseData insertNoticeDetailInfo(@RequestBody Notice notice,@RequestParam String token){

        ResponseData res = new ResponseData();
        DecodedJWT verify = JwtUtil.verify(token);
        notice.setOperatorId(Integer.valueOf(verify.getClaim("id").asString()));
        boolean result= noticeService.insertNoticeDetailInfo(notice);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else {
            res.setCode(50000);
            res.setData("fail");
        }

        return res;
    }

    @PostMapping("/saveNoticeDetailInfo")
    public ResponseData saveNoticeDetailInfo(@RequestBody Notice notice,@RequestParam String token){

        ResponseData res = new ResponseData();
        DecodedJWT verify = JwtUtil.verify(token);
        notice.setOperatorId(Integer.valueOf(verify.getClaim("id").asString()));
        boolean result= noticeService.saveNoticeDetailInfo(notice);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else {
            res.setCode(50000);
            res.setData("fail");
        }

        return res;
    }

    @PostMapping("/deleteNoticeInfo")
    public ResponseData deleteNoticeInfo( @RequestParam Integer id){
        ResponseData res = new ResponseData();
        boolean result = noticeService.removeById(id);
        if (result){
            res.setCode(20000);
            res.setData("success");

        }else{
            res.setCode(50000);
            res.setData("fail");
        }
        return res;

    }


}

